1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include <iostream> #include <opencv2/opencv.hpp> #include"Salt.h"
using namespace std; using namespace cv;
void myfilter(int filter_size,Mat& img_input,Mat& img_output);
int main(){ Mat img,img_gray,img_out,img_out2; img = imread("/home/v/home.png"); if (img.empty()){ cout<<"can't open the image"<<endl; return -1; } imshow("img",img); cvtColor(img,img_gray,COLOR_BGR2GRAY); Salt(img_gray,1000); imshow("img_gray",img_gray);
int a= 7; GaussianBlur(img_gray,img_out,Size(a,a),2,2); imshow("GaussianBlur",img_out);
myfilter(a,img_gray,img_out2); imshow("myfilter",img_out2); waitKey(0); return 0; }
void myfilter(int filter_size,Mat& img_input,Mat& img_output){
img_output = img_input.clone(); int k = (filter_size-1)/2;
for (int i = k; i < img_input.rows-k; i++){ for (int j = k; j < img_input.cols-k; j++){ double sum = 0.0; double sum1 = 0.0; double sigma = 7; double g;
for (int m = -k; m <= k; m++){ for (int n = -k; n <= k; n++){ g = exp(-(m*m+n*n)/(2*sigma*sigma)); sum += g*img_input.at<uchar>(i+m,j+n); sum1 += g; } } img_output.at<uchar>(i,j) = (int)(sum/sum1); } }
}
|